package com.nexgo.payment.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.KeyEvent;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.SettleOpType;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.constrant.UploadType;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.fragment.left.LeftFragment;
import com.nexgo.payment.fragment.main.MainFragment;
import com.nexgo.payment.trans.cardhint.CardHintActivity;
import com.nexgo.payment.trans.detail.TransDetailActivity;
import com.nexgo.payment.trans.download.DownloadTransActivity;
import com.nexgo.payment.trans.login.LoginActivity;
import com.nexgo.payment.trans.mainfaceupload.UploadActivity;
import com.nexgo.payment.trans.settle.SettleActivity;
import com.nexgo.payment.util.BarUtils;
import com.nexgo.payment.widget.DragLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Kol(Fang Qiang) on 2017/7/30.//xiaox 2017/9/20 上午11:49
 * usage:
 * modify by 谭忠扬-YuriTam
 */
public class MainActivity extends FragmentActivity implements MainContract.View {
    private static final String TAG = MainActivity.class.getSimpleName();
    private final static int REQUEST_CODE = 0;
    @BindView(R.id.draglayout)
    DragLayout mDragLayout;

    private LeftFragment leftFragment;
    private MainFragment mainFragment;

    private MainContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setTransparentStatusBar21(this);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initFragment();

        new MainPresenter(this, Repository.getInstance());
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN){
            if(keyCode == KeyEvent.KEYCODE_BACK){
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    private void initFragment() {

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();

        leftFragment = new LeftFragment();
        mainFragment = new MainFragment();

        transaction.replace(R.id.contentLeft, leftFragment, "");
        transaction.replace(R.id.contentMain, mainFragment, "");
        transaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.destroy();
    }

    @Override
    public void setPresenter(MainContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showCardExist() {
        startActivity(CardHintActivity.newInstance(this));
    }

    @Override
    public void showNeedLoadAid() {
        startActivityForResult(DownloadTransActivity.newInstance(this, TransType.AID_DOWN, true), REQUEST_CODE);
    }

    @Override
    public void showNeedLoadCapk() {
        startActivityForResult(DownloadTransActivity.newInstance(this, TransType.CAPK_DOWN, true), REQUEST_CODE);
    }

    @Override
    public void showUpload(UploadType uploadType) {
        startActivityForResult(UploadActivity.newInstance(this, uploadType), REQUEST_CODE);
    }

    @Override
    public void showNeedBatchUpload() {
        TextDialogFragment textDialogFragment = new TextDialogFragment();
        textDialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_INFO);
        textDialogFragment.setTitleText(getString(R.string.kindly_tip));
        textDialogFragment.setContentText(getString(R.string.now_batch_upload), Gravity.CENTER);
        textDialogFragment.setNegativeText(getString(R.string.cancel));
        textDialogFragment.setPositionText(getString(R.string.confirm));
        textDialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {

            }

            @Override
            public void onConfirm() {
                startActivity(SettleActivity.newInstance(MainActivity.this, SettleOpType.BATCH_UPLOAD));
            }
        });
        textDialogFragment.show(getSupportFragmentManager(), null);
    }


    @Override
    public void showNeedQpsBinB() {
        startActivityForResult(DownloadTransActivity.newInstance(this, TransType.QPS_BIN_B_DOWN, true), REQUEST_CODE);
    }

    @Override
    public void showNeedQpsBinC() {
        startActivityForResult(DownloadTransActivity.newInstance(this, TransType.QPS_BIN_C_DOWN, true), REQUEST_CODE);
    }

    @Override
    public void showNeedParamDown() {
        startActivityForResult(DownloadTransActivity.newInstance(this, TransType.PARAM_DOWN, true), REQUEST_CODE);
    }

    @Override
    public void showNeedUploadState() {
        startActivityForResult(DownloadTransActivity.newInstance(this, TransType.UPLOAD_STATE, true), REQUEST_CODE);
    }

    @Override
    public void showNeedLogin() {
        startActivity(LoginActivity.newInstance(this, true));
    }

    @Override
    public void showNeedBlacklistDown() {
        startActivityForResult(DownloadTransActivity.newInstance(this, TransType.BLACKLIST_DOWN, true), REQUEST_CODE);
    }

    @Override
    public void showNeedRfParamDown() {
        startActivityForResult(DownloadTransActivity.newInstance(this, TransType.RF_PARAM_DOWN, true), REQUEST_CODE);
    }

    @Override
    public void showNeedPrintLastTrans() {
        startActivity(TransDetailActivity.newInstance(this, null));
    }

    @Override
    public void showNeedPrintLastSettle() {
        startActivity(SettleActivity.newInstance(this, SettleOpType.REPRINT_SETTLE_INFO));
    }

    /**
     * 打开左滑界面
     */
    public void open() {
        mDragLayout.open(true);
    }

    /**
     * 关闭侧滑
     */
    public void close() {
        mDragLayout.close();
    }

    @Override
    public void onBackPressed() {
        // 如果此时左边菜单是开启的状态，则先恢复
        if (mDragLayout.getStatus() == DragLayout.Status.Open) {
            mDragLayout.close();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE:
                mPresenter.setUploadResult(resultCode == 0);
                break;
        }
    }
}
